﻿using System;

namespace YinLongTool.Number
{
    public static class DoubleUtil
    {
        /// <summary>
        /// 将小数截断为8位
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static double Digits8( double num)
        {
            return (long)(num * 1E+8) * 1e-8;
        }  
    
       
        /// <summary>
        /// 将int转换成double
        /// </summary>
        /// <param name="num">int类型</param>
        /// <returns>int类型</returns>
        public static double ToDouble(int num)
        {
            return num * 1.0;
        }
        /// <summary>
        /// 字符串转double
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <returns>double类型的数据</returns>
        public static double ToDouble(decimal s)
        {
            return (double)s;
        } 
        /// <summary>
        /// 字符串转double
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <returns>double类型的数据</returns>
        public static double ToDouble(string s)
        {
            Double.TryParse(s, out var result);
            return result;
        }
        /// <summary>
        /// 字符串转double
        /// </summary>
        /// <param name="s">源字符串</param>
        /// <param name="defaultValue">转换失败的默认值</param>
        /// <returns>double类型的数据</returns>
        public static double ToDouble(string s, int defaultValue = 0)
        {
            return double.TryParse(s, out var result) ? result : defaultValue;
        }

    }
}